package com.yatong.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yatong.entity.Weather;
import com.yatong.entity.WeatherForecast;
import com.yatong.entity.WeatherYesterday;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @program: YaTong
 * @description: 通过接口获取天气数据
 * @author: Mr.Guo
 * @create: 2018-09-21 11:27
 **/
public class ReturnWeatherController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private static final String uri = "http://t.weather.sojson.com/api/weather/city/101030100";


    private static String getJsonData(/*String city*/){
        //去 服务器 请求一个数据
        RestTemplate rest = new RestTemplate();
        //ResponseEntity<T>是Spring对HTTP请求响应的封装，
        //String.class，表示希望返回的body类型是String
        ResponseEntity<String> response = rest.getForEntity(uri /*+ city*/, String.class);

        //查询为成功的状态码
        if (response.getStatusCodeValue() == 200){
            return response.getBody();
        }
        return null;
    }

    public static void main(String[] args) {
        String jsondata = getJsonData(/*"天津"*/);
        try{
            JSONObject jsonObject = JSON.parseObject(jsondata);
            String data = jsonObject.get("data").toString();
            JSONObject jsonObject1 = JSON.parseObject(data);


            Weather weather = new Weather();
            weather.setDate(jsonObject.get("date").toString());
            weather.setCity(jsonObject.get("city").toString());
            weather.setShidu(jsonObject1.get("shidu").toString());
            weather.setPm25(jsonObject1.get("pm25").toString());
            weather.setPm10(jsonObject1.get("pm10").toString());
            weather.setQuality(jsonObject1.get("quality").toString());
            weather.setWendu(jsonObject1.get("wendu").toString());
            weather.setGanmao(jsonObject1.get("ganmao").toString());

            System.out.println("今天天气"+weather.toString());
            System.out.println("*************************************");

            String yesterday = jsonObject1.get("yesterday").toString();
            JSONObject jsonObject2 = JSON.parseObject(yesterday);
            WeatherYesterday weatherYesterday = new WeatherYesterday();
            weatherYesterday.setDate(jsonObject2.get("date").toString());
            weatherYesterday.setSunrise(jsonObject2.get("sunrise").toString());
            weatherYesterday.setHigh(jsonObject2.get("high").toString());
            weatherYesterday.setLow(jsonObject2.get("low").toString());
            weatherYesterday.setSunset(jsonObject2.get("sunset").toString());
            weatherYesterday.setAqi(jsonObject2.get("aqi").toString());
            weatherYesterday.setFx(jsonObject2.get("fx").toString());
            weatherYesterday.setFl(jsonObject2.get("fl").toString());
            weatherYesterday.setType(jsonObject2.get("type").toString());
            weatherYesterday.setNotice(jsonObject2.get("notice").toString());

            System.out.println("昨天天气"+weatherYesterday.toString());


            System.out.println("*************************************");

            String forecast = jsonObject1.get("forecast").toString();
            List<WeatherForecast> weatherForecasts = JSON.parseArray(forecast, WeatherForecast.class);
            for (WeatherForecast weatherForecast :weatherForecasts) {
                System.out.println(weatherForecast.toString());
            }

        }catch (Exception e){
            e.printStackTrace();
//            log.info("错误");
        }

//        List<Weather> stu_list = new ArrayList<Weather>(JSON.parseArray(JSON.parseObject(jsondata).get("data").toString(), Weather.class));
//        for (Weather weather: stu_list) {
//            System.out.println(weather);
//
//        }

//        JSONObject jsonObject = JSON.parseObject(jsondata);
//        Object data = jsonObject.get("data");
//        System.out.println(data);
//        Object data1 = jsonObject.get("forecast");
//        System.out.println(data1);

    }

}
